<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { BaseFormConfig } from '../../config'
import { getAreaObj ,getAgeFromIdCard} from '@/utils/util'
import { cloneDeep } from 'lodash-es'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { getValue } = useOperate(1)
const { setTempParams } = useCommonStore()

const rowData: any = inject('nowRowData')
let dpServiceObjects = rowData.value?.data?.dpServiceObjects

const emit = defineEmits(['submit'])

let baseData = rowData.value?.baseData?.dpServiceObjects
const formModel = ref({
	...cloneDeep(dpServiceObjects),
})

const formRef = ref()
const onSubmit = (form, done) => {
	const areaObj: any = getAreaObj(form.areas)
	const areaNameObj: any = getAreaObj(form.areas_name)
	const crAreasObj: any = getAreaObj(form.crAreas, 'cr')
	const crAreasNameObj: any = getAreaObj(form.cr_areas, 'cr')

	let buildObj = {}
	if (form.buildInfo) {
		const buildName = form.buildInfoName.split('/')
		buildObj = {
			checkinBuildingId: form.buildInfo[0],
			checkinFloor: form.buildInfo[1],
			checkinRoomId: form.buildInfo[2],
			checkinBedCodeId: form.buildInfo[3],

			checkinBuilding: buildName[0],
			checkinRoom: buildName[2],
			checkinBedCode: buildName[3],
		}
	}

	const params = {
		...form,
		...areaObj,
		...crAreasObj,
		...areaNameObj,
		...crAreasNameObj,
		...buildObj,
		buildInfo: undefined,
		buildInfoName: undefined,
		code: 1001,
	}
	if (form.buildInfo) {
		params.moveInBuilding = form.buildInfo.join(',')
	}
	if (params.elderlyType && params.idCard) {
		const _age = getAgeFromIdCard(params.idCard)
		if (params.elderlyTypeLabel.includes('80周岁及以上老年人') && _age < 80) {
			ElMessage({
				type: 'warning',
				message: '服务对象类别与年龄不匹配',
			})
			done()
			return
		}
		if (params.elderlyTypeLabel.includes('百岁老人') && _age < 100) {
			ElMessage({
				type: 'warning',
				message: '服务对象类别与年龄不匹配',
			})
			done()
			return
		}
		if (params.elderlyTypeLabel.includes('离休老干部') && _age < 55) {
			ElMessage({
				type: 'warning',
				message: '服务对象类别与年龄不匹配',
			})
			done()
			return
		}
		if (
			(params.elderlyTypeLabel.includes('老年人') ||
				params.elderlyTypeLabel.includes('失能险老人') ||
				params.elderlyTypeLabel.includes('孤寡老人')) &&
			_age < 60
		) {
			ElMessage({
				type: 'warning',
				message: '服务对象类别与年龄不匹配',
			})
			done()
			return
		}
	}
	// 变更字段
	let changeColumnsBase = BaseFormConfig.column.filter((item) => item.changeField)
	// 处理变更数据
	let changeData = []
	changeColumnsBase.map((item) => {
		const tempBefore = baseData[item.propName || item.prop]
		const tempAfter = params[item.propName || item.prop]
		if (tempBefore != tempAfter) {
			changeData.push({
				label: item.label,
				before: getValue(tempBefore, item),
				after: getValue(tempAfter, item),
			})
		}
	})
	const emptyObj = {
		areas_name: undefined,
		cr_areas: undefined,
		areas: undefined,
		crAreas: undefined,
		code: undefined,
	}
	const changeObj = {
		data: changeData,
		dataChangeContentList: [
			{
				changeBefore: {
					...baseData,
					...emptyObj,
				},
				changeAfter: {
					...params,
					...emptyObj,
				},
				changeData: {
					...params,
					...emptyObj,
				},
				changeType: '服务对象',
				code: 1001,
			},
		],
	}
	setTempParams({
		name: params.name,
		idCard: params.idCard,
		nowChange: changeObj,
	})
	emit('submit', changeObj)
	done()
}

const onSave = () => {
	formRef.value!.onSubmit()
}

defineExpose({
	onSave,
})
</script>

<style scoped></style>
